<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#/goods">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }">商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>编辑商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 提示信息 -->
      <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>
      <!-- 步骤条 -->
      <el-steps :active="active - 1" align-center finish-status="success">
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 表单选项 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" label-position="top">
        <!-- tab选项卡 -->
        <el-tabs v-model="active" @tab-click="handleClick" :before-leave="beforeLeaveHandle" tab-position="left">
          <!-- 基本信息 -->
          <el-tab-pane label="商品名称" name="1">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model.number="addForm.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model.number="addForm.goods_weight" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addForm.goods_number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader v-model="selectedCatIds" ref="cateHandle" :options="$store.state.cate.allCateTreeList"
                :props="cateProps" @change="handleChanged" clearable></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="2">
            <!-- 商品参数 -->
            <el-form-item :label="item.attr_name" v-for="(item, index) in manyData" :key="index">
              <el-checkbox-group v-model="checkedSku" v-if="item.attr_vals.length">
                <el-checkbox checked @change="changeSku($event, { attr_id: item.attr_id, attr_value: item1 })"
                  v-for="item1 in item.attr_vals" :key="item1" :label="item1" border></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="3">
            <el-form-item :label="item.attr_name" v-for="(item, index) in onlyData" :key="index">
              <template v-if="item.attr_vals.length === 1">
                <!-- 如果attr_vals值为空数组，就使用input框显示 -->
                <el-input v-model="item.attr_vals[0]" @blur="changeAttr($event, item.attr_id)"></el-input>
              </template>
              <template v-else>
                <!-- 否则使用下拉菜单显示 -->
                <!-- optionsValues对象：{3077:'北京'}或者是{3067:'TCL电视'} -->
                <el-select v-model="optionsValues[item.attr_id]">
                  <el-option v-for="item1 in item.attr_vals" :key="item1" :label="item1" :value="item1">
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="4">
            <!-- action:图片上传的后台api接口地址 -->
            <!-- list-type="picture":预览图片的效果 -->
            <!-- headers:添加请求头部，手动添加请求头部，保存到data中 -->
            <el-upload :action="upLoadUrl" list-type="picture" :headers="headerObj" :on-success="handleSuccess"
              :before-upload="checkFile" :on-progress="handleProgress" :before-remove="removeConfirm"
              :on-remove="handleRemove" :on-preview="handlePreview" multiple>
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="5">
            <!-- 富文本编辑器 -->
            <quill-editor v-model="addForm.goods_introduce"></quill-editor>
            <!-- 提交按钮 -->
            <el-button type="primary" class="btnAdd" @click="addGoods">
              添加商品
            </el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
    <!-- 实现图片的预览 -->
    <el-dialog title="图片预览" :visible.sync="previewDialogVisible" width="50%">
      <img :src="previewPath" alt="" class="previewImg" />
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import GoodsEdit from './goods-edit.js'
export default {
  ...GoodsEdit,
  components: {
    quillEditor
  }
}
</script>

<style lang="scss" scoped>
.previewImg {
  width: 100%;
}

.btnAdd {
  margin-top: 15px;
}

// 样式穿透
:deep(.ql-editor) {
  height: 300px;
}
</style>